package com.voole.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.voole.entity.SysAction;
import com.voole.entity.SysUser;
import com.voole.service.IMenuService;
import com.voole.service.ISysAuthorityService;
import com.voole.service.ISysRoleService;
import com.voole.service.ISysUserService;

@Controller
public class SysRoleController extends BaseController {

	private static Logger logger = Logger.getLogger(SysRoleController.class);
	
	@Autowired
	private IMenuService menuService;
	@Autowired
	private ISysRoleService sysRoleService;
	
	/**
	 * 配置管理 - 权限管理 - 权限和访问页面
	 */
	@RequestMapping("/sysrole/sysrole.do")
    public ModelAndView sysuser(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("sys_role");
    }
	
	/**
	 * 权限 - 分页列表
	 */
	@RequestMapping("/sysrole/findByPage.do")
	public void findByPage(HttpServletRequest request,HttpServletResponse response) {
		try {
		    String start = request.getParameter("start");//数据起始位置
		    String length = request.getParameter("length");//数据长度
		    
			Map<String, Object> ajaxMap = sysRoleService.findByPage(start, length);
			this.ajaxComplete(JSONObject.fromObject(ajaxMap).toString(), response);
			
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
    }
	
	/**
	 * 权限 - 获取单个权限
	 * (以及 权限对应的1-4级目录tree)
	 */
	@RequestMapping("/sysrole/getSysRole.do")
    public ModelAndView getSysRole(HttpServletRequest request,HttpServletResponse response) {
		try {
			String roleId = request.getParameter("roleId");
			Integer id = null;
			if(!"null".equals(roleId)){
				id = Integer.valueOf(roleId);
			}
			List<Map<String, Object>> dataList = menuService.getMenuListForJsTree(id);
			this.ajaxComplete(JSONArray.fromObject(dataList), response);
		} catch (Exception e) {
		}
		
		return null;
    }
	
	/**
	 * 权限 - 获取所有权限
	 * (用户管理页面)
	 */
	@RequestMapping("/sysrole/getSysRoleForUserPage.do")
    public ModelAndView getSysRoleForUserPage(HttpServletRequest request,HttpServletResponse response) {
		try {
			this.ajaxComplete(sysRoleService.getList(), response);
		} catch (Exception e) {}
		return null;
    }
	
	/**
	 * 权限 - 新建权限
	 */
	@RequestMapping("/sysrole/saveSysRole.do")
    public ModelAndView saveSysRole(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String roleName = request.getParameter("name");
			String roleEn = request.getParameter("role");
			result = sysRoleService.addSysRole(roleName, roleEn, 0);
		} catch (Exception e) {
			result.put("msg", e.getMessage());
		}
		this.ajaxComplete(result, response);
		return null;
    }
	
	/**
	 * 权限 - 配置权限
	 */
	@RequestMapping("/sysrole/setSysRole.do")
    public ModelAndView setSysRole(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String roleId = request.getParameter("setRoleId");
			String actionIds = request.getParameter("actionIds");
			result = sysRoleService.setSysRole(roleId, actionIds);
		} catch (Exception e) {
			result.put("msg", e.getMessage());
		}
		this.ajaxComplete(result, response);
		return null;
    }
}
